/* libfontman.h */

#ifndef __LIBFONTMAN__
#define __LIBFONTMAN__

#ifdef FONTMAN_BACKEND_FC
#define FONTMAN_BACKEND     "fontconfig"
#endif

#ifdef FONTMAN_BACKEND_ATS
#define FONTMAN_BACKEND     "ATS"
#endif

#define PATH_SEP    '/'
#include <limits.h>
#include <string.h>
#include <sys/dir.h>
#include <stdio.h>

#define FONTMAN_MAX_FONT_NAME       256
#define FONTMAN_MAX_PS_NAME         256
#define FONTMAN_MAX_LOCAL_NAME      256
#define FONTMAN_MAX_PLATFORM_ID     7
#define FONTMAN_MAX_APPLE_ENCODING  4
#define FONTMAN_MAX_MS_ENCODING     10

enum fontman_valid_encoding {
    FONTMAN_ENCODING_APPLE = 1, 
    FONTMAN_ENCODING_MS = 3, 
};

#define FONTMAN_FILE_NAME_PATTERN   "*.tt?"

enum fontman_list_type {
    FONTMAN_LIST_SYSTEM, 
    FONTMAN_LIST_PATH, 
    FONTMAN_LIST_TEX_FONT, 
};

enum fontman_info_type {
    FONTMAN_INFO_SYSTEM, 
    FONTMAN_INFO_FILE, 
    FONTMAN_INFO_TEX_FONT, 
};

enum fontman_error {
    FONTMAN_ERR_PLATFORM_INIT = 1, 
    FONTMAN_ERR_PATH_OPEN, 
    FONTMAN_ERR_NO_NEXT, 
    FONTMAN_ERR_GET_PATH, 
    FONTMAN_ERR_GET_NAME, 
    FONTMAN_ERR_GET_PS_NAME, 
    FONTMAN_ERR_INIT_FREETYPE,
    FONTMAN_ERR_OPEN_FTFACE, 
    FONTMAN_ERR_NOT_SUPPORTED, 
    FONTMAN_ERR_FIND_FONT, 

    FONTMAN_ERR_MAX
};

enum fontman_list_flags {
    FONTMAN_GET_NAME        = 1, 
    FONTMAN_GET_PS_NAME     = 2, 
    FONTMAN_GET_PATH        = 4,
    FONTMAN_GET_LOCAL_NAME  = 8, 
    FONTMAN_INCLUDE_ALL     = 16, 
    FONTMAN_GET_EXTRA_INFO  = 32, 
    FONTMAN_GET_ALL         = ~0
};

typedef struct fontman_charmap {
    unsigned short pid;
    unsigned short eid;
} fontman_charmap_t;

typedef struct fontman_info {
    long num_glyphs;

    char family[FONTMAN_MAX_FONT_NAME];
    char style[FONTMAN_MAX_FONT_NAME];

    int  num_charmaps;
    fontman_charmap_t **charmaps;
} fontman_info_t;

/* represent a cross platform font face */
typedef struct _fontman_face {
    char path[PATH_MAX];
    char name[FONTMAN_MAX_FONT_NAME];
    char ps_name[FONTMAN_MAX_PS_NAME];
    char local_name[FONTMAN_MAX_LOCAL_NAME];
    fontman_info_t *extra;
} fontman_face_t;

int  fontman_list_init(enum fontman_list_type type, 
                       const char *path, 
                       const char *pattern, 
                       unsigned int flags);
int  fontman_list_next(fontman_face_t *face);
void fontman_list_free();

int  fontman_list_platform_init();
void fontman_list_platform_free();
int  fontman_list_platform_next(fontman_face_t *face);

int  fontman_list_dir_init(const char *path);
int  fontman_list_dir_next(char *path, int max_len);
void fontman_list_dir_free();

int  fontman_list_pattern_matched(const char *str);

int  fontman_info(fontman_face_t *face, 
                  enum fontman_info_type type);
int  fontman_info_file(fontman_face_t *face, unsigned int flags);
int  fontman_info_platform_font(fontman_face_t *face);
void fontman_info_done(fontman_face_t *face);
const char *fontman_error_msg(int error);
const char *fontman_get_platform(unsigned short pid);
const char *fontman_get_apple_encoding(unsigned short eid);
const char *fontman_get_ms_encoding(unsigned short eid);

const char *fontman_get_backend();

#endif

